#ifndef __HELPER_H__
#define __HELPER_H__

#include "nemu.h"
#include "cpu/decode/operand.h"
#include "cpu/eflags.h" 

extern uint8_t current_sreg;

/* All function defined with 'make_helper' return the length of the operation. */
#define make_helper(name) int name(swaddr_t eip)

//从目标地址读取指令
static inline uint32_t instr_fetch(swaddr_t addr, size_t len) {
	uint8_t last_sreg = current_sreg;
	current_sreg = R_CS;
	uint32_t ret = swaddr_read(addr, len);
	current_sreg = last_sreg;
	return ret;
}

/* Instruction Decode and EXecute */
//指令解析与执行
static inline int idex(swaddr_t eip, int (*decode)(swaddr_t), void (*execute) (void)) {
	/* eip is pointing to the opcode */
	int len = decode(eip + 1);		//解析操作码，返回指令长度
	execute();		//执行解析后的指令
	return len + 1;	// "1" for opcode		//返回包括操作码本身的总长度
}

/* shared by all helper function */
extern Operands ops_decoded;

#define op_src (&ops_decoded.src)		//源操作数指针
#define op_src2 (&ops_decoded.src2)		//第二个源操作数指针
#define op_dest (&ops_decoded.dest)		//目的操作数指针


#endif
